/*
	The Shadow Framework - a complete framework for Real Time Graphics based on OpenGL
    Copyright (C) 2008  Alessandro Martinelli  <alessandro.martinelli@unipv.it>

    This file is part of The Shadow Framework.

    The Shadow Framework is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Shadow Framework is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Shadow Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

package objLoader.math;

import java.util.ArrayList;

public abstract class LinearMeanSquareProblem {

	private int dimension;
	private LinearSystem system;
	private float[] sol;
	//private float[][] solV=new float[0][];
	private ArrayList<float[]> valss=new ArrayList<float[]>();

	public LinearMeanSquareProblem(int dimension) {
		super();
		this.dimension = dimension;
		system=new LinearSystem(dimension);
		sol=new float[dimension];
	}
	
	public abstract float evalFunction(float data[],int index);
	
	public void addEvaluation(float x[],float y){
		float[] vals=new float[dimension+1];
		for(int i=0;i<dimension;i++)
			vals[i]=evalFunction(x, i);
		
		for(int i=0;i<dimension;i++){
			for(int j=0;j<dimension;j++){
				system.setA(i, j,system.getA(i, j) + vals[i]*vals[j]);
			}
			system.setb(i, system.getb(i)+vals[i]*y);
		}
		vals[dimension]=y;
		//this.valss.add(vals);
	}
	
	public float[] getSolution(){
		system.solveSystem();
		sol=system.getSol();
		//solV=system.getSolV();
		return sol;
	}
	
	public float getError(){
		float sum=0;
		
		for(int i=0;i<valss.size();i++){
			float[] vals=(float[])(valss.get(i));
			float del=0;
			for(int j=0;j<vals.length-1;j++){
				del+=sol[j]*vals[j];
			}
			del-=vals[dimension];
			sum+=del*del;
		}
		
		return sum;
	}
}
